//
//  UIView+AddView.m
//  Pods
//
//  Created by Apple on 2022/11/4.
//

#import "UIView+AddView.h"

@implementation UIView (AddView)

- (void)addToView:(UIView *)aView atIndex:(NSInteger)index edgeInsets:(UIEdgeInsets)edgeInsets {
    if (aView) {
        if (index >= 0) {
            [aView insertSubview:self atIndex:index];
        } else {
            [aView addSubview:self];
        }
        [self setTranslatesAutoresizingMaskIntoConstraints:NO];
        
        [self.topAnchor     constraintEqualToAnchor:aView.topAnchor    constant:edgeInsets.top].active     = YES;
        [self.leftAnchor    constraintEqualToAnchor:aView.leftAnchor   constant:edgeInsets.left].active    = YES;
        [aView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor  constant:edgeInsets.bottom].active  = YES;
        [aView.rightAnchor  constraintEqualToAnchor:self.rightAnchor   constant:edgeInsets.right].active   = YES;
    }
}

- (void)addToView:(UIView *)aView edgeInsets:(UIEdgeInsets)edgeInsets {
    [self addToView:aView atIndex:-1 edgeInsets:edgeInsets];
}

- (void)addToView:(UIView *)aView {
    [self addToView:aView atIndex:-1 edgeInsets:UIEdgeInsetsZero];
}

- (void)insertToView:(UIView *)aView atIndex:(NSInteger)index edgeInsets:(UIEdgeInsets)edgeInsets {
    [self addToView:aView atIndex:index edgeInsets:edgeInsets];
}

- (void)insertToView:(UIView *)aView atIndex:(NSInteger)index {
    [self addToView:aView atIndex:index edgeInsets:UIEdgeInsetsZero];
}

- (void)insertToView:(UIView *)aView edgeInsets:(UIEdgeInsets)edgeInsets {
    [self addToView:aView atIndex:0 edgeInsets:edgeInsets];
}

- (void)insertToView:(UIView *)aView {
    [self addToView:aView atIndex:0 edgeInsets:UIEdgeInsetsZero];
}

@end
